var app = getApp();
var listOffset;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoHgt: 0,
    newsList: [],
    blankshow: false,
    hasMore: true,
    blanktips: '~敬请期待哦~',
    newsLoaded: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.showLoading();
  },
  onShow:function(){
    app.ready(() => {
      listOffset = 0;
      this.loadNewslist(listOffset);
    });
    app.error(err => {
      if (!app.globalData.userWxInfo) {
        wx.navigateTo({
          url: '/pages/auth/auth',
        });
      }
    });
  },
  loadNewslist: function(offset){
    var url = '/api/Article/articleList';
    var params = {
      page: offset
    };
    app.request(url,params).then(this.loadNewsListCallBack.bind(this));
  },
  loadNewsListCallBack: function(res){
    this.setData({
      newsLoaded: true,
    });
    app.hideLoading();
    if(res.status != 'ok'){
      return;
    }
    var news_arr = [];
    var temp_arr = res.result;
    if(listOffset > 0){
      news_arr = this.data.newsList;
    }
    for(var i=0;i<temp_arr.length;i++){
      if (temp_arr[i].thumb){
        temp_arr[i].thumb = app.globalData.webUrl + temp_arr[i].thumb;
      }
      temp_arr[i].add_time = app.formatDateTime(temp_arr[i].add_time,false);
      news_arr.push(temp_arr[i]);
    }
    
    this.setData({
      newsList:news_arr,
      blankshow: (news_arr.length > 0 ? false:true)
    });
  },
  toNewsDetail:function(e){
    var newsid = e.currentTarget.dataset.newsid;
    var path = '../news/news?newsid=' + newsid;
    wx.navigateTo({
      url: path,
    });
  },
  onReachBottom:function(){
    app.showLoading();
    listOffset++;
    this.loadNewslist(listOffset);
  },
  onPullDownRefresh:function(){
    listOffset = 0;
    this.loadNewslist(listOffset);
    wx.stopPullDownRefresh();
  },
  onShareAppMessage: function () {
    return app.shareToIndex();
  }
})